package com.tsugun.mediaplayer.view.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.tsugun.mediaplayer.R;
import com.tsugun.mediaplayer.model.entity.MusicModel;
import com.tsugun.mediaplayer.presenter.provider.ListProvider;
import com.tsugun.mediaplayer.presenter.utils.SettingPreferenceHelper;
import com.tsugun.mediaplayer.view.activity.IndexActivity;
import com.tsugun.mediaplayer.view.fragment.subfragment.SongListFragment;
import com.tsugun.mediaplayer.view.widget.SlidingRelativeLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 专辑界面
 * Created by shize on 2017/4/20.
 */

public class LatelyFragment extends BaseFragment {

    @BindView(R.id.id_sliding_body)
    SlidingRelativeLayout mSlidingLayout;
    @BindView(R.id.id_bg_night)
    RelativeLayout mBgLayout;
    @BindView(R.id.id_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.id_lately_list)
    FrameLayout mFLSongList;

    private View mFragment;
    private Context mContext;
    private IndexActivity indexActivity;
    private Unbinder unbinder;
    private ListProvider mProvider;
    private SongListFragment mSongFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mFragment = inflater.inflate(R.layout.fragment_lately, container, false);
        mContext = container.getContext();
        unbinder = ButterKnife.bind(this, mFragment);

        initData();
        bindData();
        return mFragment;
    }

    @Override
    protected void setSettingTheme() {
        int themeColor = indexActivity.getThemeColor();
        mToolbar.setBackgroundColor(themeColor);
        mBgLayout.setBackgroundColor(SettingPreferenceHelper.INSTANCE.getBACKGROUND_COLOR());
    }

    /**
     * 初始化数据
     */
    private void initData() {
        indexActivity = (IndexActivity) mContext;
        mProvider = indexActivity.getListProvider();
        mSongFragment = new SongListFragment();
    }

    /**
     * 绑定数据到控件
     */
    private void bindData() {
        bindSliding();
        bindToolbar();
        bindSongList();
    }

    private void bindToolbar() {
        mToolbar.setTitle(getString(R.string.str_lately));
        mToolbar.setNavigationIcon(R.drawable.ic_arrow_back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                indexActivity.getMusicFragmentManager().backFragment();
            }
        });
    }

    /**
     * 绑定滑动
     */
    private void bindSliding() {
        mSlidingLayout.setOnSlidingListener(new SlidingRelativeLayout.OnSlidingListener() {
            @Override
            public void onDown() {

            }

            @Override
            public void onAnimationEnd(boolean isClose) {
                if (isClose) {
                    indexActivity.getMusicFragmentManager().backFragment();
                }
            }
        });
    }

    /**
     * 初始化歌曲列表
     */
    private void bindSongList() {
        FragmentTransaction fragmentTransaction = indexActivity.getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.id_lately_list, mSongFragment);
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        fragmentTransaction.commit();
        mSongFragment.setResult((List<MusicModel>) mProvider.getLately());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }
}
